package com.prs.crm.action.hr.report;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.jfree.data.category.DefaultCategoryDataset;

import com.prs.crm.action.ReportAction;
import com.prs.crm.context.Constrains;
import com.prs.crm.domain.hr.Payroll;
import com.prs.crm.domain.hr.PayrollBill;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.report.ChartResult;
import com.prs.crm.report.ChartService;
import com.prs.crm.service.hr.PayrollBillService;
import com.prs.crm.service.hr.PayrollService;
import com.prs.crm.service.hr.PersonService;

public class SalaryAnalysisAction extends ReportAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = -7601458451075688580L;
	private PaginationSupport pagination;
	private PayrollService payrollService;
	private PayrollBillService payrollBillService;
	private PersonService personService;
	private ChartService chartService;
	private Payroll payroll;
	private Collection<Person> payrollPersons;
	private ChartResult chartResult;
	private Person person;
	private List<PayrollBill> currentPayrollBills;
	
	private int exportType;
	private int GraphicsType;
	private String category;
	
	/**
	 * 增项总计
	 */
	private double increaseValue;
	/**
	 * 减项总计
	 */
	private double reduceValue;
	/**
	 * 实发总计
	 */
	private double authenticValue;

	@Action("listSalary")
	public String listSalary(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Payroll.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		criteria.addOrder(Order.desc("recordTime"));
		this.setPagination(this.getPayrollService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	
	@Action("salaryDistribution")
	public String salaryDistribution(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Payroll.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		criteria.addOrder(Order.desc("recordTime"));
		this.setPagination(this.getPayrollService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@SuppressWarnings("unchecked")
	@Action("viewDistribution")
	public String viewDistribution(){
		reduceValue = 0;
		increaseValue = 0;
		List<PayrollBill> increaseItems = this.getPayrollBillService().getIncreaseItem(payroll);
		List<PayrollBill> reduceItems = this.getPayrollBillService().getReduceItem(payroll);
		
		for(PayrollBill bill : increaseItems) {
			increaseValue += bill.getValue();
		}
		for(PayrollBill bill : reduceItems) {
			reduceValue += bill.getValue();
		}
		
		authenticValue = increaseValue - reduceValue;
		
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(Person.class);
		criteria.add(Restrictions.in("id", this.getPayrollBillService()
				.findPersonIds(payroll)));
		payrollPersons = this.getPersonService().find(criteria);
		
		Collection<Object[]> dataList = (Collection<Object[]>)this.getPayrollBillService().getReportDataSource(payrollPersons, payroll);
		String[] level = {"￥1K以下","1K-2K","2K-3K","3K-4K","4K-5K","5K-6K","6K-7K","7K-8K","8K-9K","9K-1K","￥1W以上"};
		Map<String,Integer> datas = new TreeMap<String, Integer>();
		for(String item : level){
			datas.put(item, 0);
		}
		for(Object[] o : dataList){
			for(Object value : o){
				if(value instanceof Number){
					double tm = Math.floor((Double)value/1000);
					String key = tm >= 10 ? level[10] : level[(int)tm];
					datas.put(key, datas.get(key) + 1);
				}
			}
		}
		DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
		for(String key : datas.keySet()){
			dataSet.addValue(datas.get(key), key, key);
		}
		chartService.setWidth(700);
		if(this.getGraphicsType() == Constrains.REPORT_BARCHART){
			chartResult = this.getChartService().export(dataSet, "薪酬分布统计", "薪水", "", "salaryInfo");
		}
		
		return SUCCESS;
	}
	
	
	
	@SuppressWarnings("unchecked")
	@Action("viewSalary")
	public String viewSalary(){
		reduceValue = 0;
		increaseValue = 0;
		List<PayrollBill> increaseItems = this.getPayrollBillService().getIncreaseItem(payroll);
		List<PayrollBill> reduceItems = this.getPayrollBillService().getReduceItem(payroll);
		
		for(PayrollBill bill : increaseItems) {
			increaseValue += bill.getValue();
		}
		for(PayrollBill bill : reduceItems) {
			reduceValue += bill.getValue();
		}
		
		authenticValue = increaseValue - reduceValue;
		
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(Person.class);
		criteria.add(Restrictions.in("id", this.getPayrollBillService()
				.findPersonIds(payroll)));
		payrollPersons = this.getPersonService().find(criteria);
		
		Collection<Object[]> dataList = (Collection<Object[]>)this.getPayrollBillService().getReportDataSource(payrollPersons, payroll);
		if(this.getGraphicsType() == Constrains.REPORT_PIECHART){
			chartResult = this.getChartService().export(this.getChartService().fillDataset(dataList, GraphicsType), "薪酬分析", "salaryInfo");
		}else {
			chartResult = this.getChartService().export(this.getChartService().fillDataset(dataList, GraphicsType), "薪酬分析", "薪水", "", "salaryInfo");
		}
		
		return SUCCESS;
	}
	@Action("salaryInfo")
	public String salaryInfo(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(Person.class);
		criteria.add(Restrictions.eq("id", this.getChartService().getPerson(this.getCategory()).getId()));
		payrollPersons = this.getPersonService().find(criteria);
		person = payrollPersons.iterator().next();
		reduceValue = 0;
		increaseValue = 0;
		List<PayrollBill> increaseItems = this.getPayrollBillService().getIncreaseItem(payroll,person);
		List<PayrollBill> reduceItems = this.getPayrollBillService().getReduceItem(payroll,person);
		
		for(PayrollBill bill : increaseItems) {
			increaseValue += bill.getValue();
		}
		for(PayrollBill bill : reduceItems) {
			reduceValue += bill.getValue();
		}
		
		authenticValue = increaseValue - reduceValue;
		
		this.currentPayrollBills = this.getPayrollBillService().getCurrentPayrollBill(person, payroll);
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	@Action(value="exportSalaryReport", results = { @Result(name = "success"
		,type="stream",params={"contentType","${contentType}","inputName","inputStream"
		,"bufferSize","1024","contentDisposition","filename=${outfileName}"}) })
	public String exportSalaryReport(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(Person.class);
		criteria.add(Restrictions.in("id", this.getPayrollBillService().findPersonIds(payroll)));
		payrollPersons = this.getPersonService().find(criteria);
		
		Collection<PayrollBill> dataList = (Collection<PayrollBill>)this.getPayrollBillService().findAllPayrollBill(payrollPersons, payroll);
		
		this.setOutFilePath(this.getPayrollBillService().export(dataList, (List)payrollPersons, this.getExportType(), payroll.getTitle()));
		return SUCCESS;
	}
	
	public int getExportType() {
		return exportType;
	}
	public void setExportType(int exportType) {
		this.exportType = exportType;
	}
	public List<PayrollBill> getCurrentPayrollBills() {
		return currentPayrollBills;
	}
	public void setCurrentPayrollBills(List<PayrollBill> currentPayrollBills) {
		this.currentPayrollBills = currentPayrollBills;
	}
	public Person getPerson() {
		return person;
	}
	public void setPerson(Person person) {
		this.person = person;
	}
	public String getCategory() {
		return category;
	}
	public void setCategory(String category) {
		this.category = category;
	}
	public ChartResult getChartResult() {
		return chartResult;
	}
	public void setChartResult(ChartResult chartResult) {
		this.chartResult = chartResult;
	}
	public PayrollBillService getPayrollBillService() {
		return payrollBillService;
	}
	public double getIncreaseValue() {
		return increaseValue;
	}
	public double getReduceValue() {
		return reduceValue;
	}
	public void setPayrollBillService(PayrollBillService payrollBillService) {
		this.payrollBillService = payrollBillService;
	}
	public void setIncreaseValue(double increaseValue) {
		this.increaseValue = increaseValue;
	}
	public void setReduceValue(double reduceValue) {
		this.reduceValue = reduceValue;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public PersonService getPersonService() {
		return personService;
	}
	public Payroll getPayroll() {
		return payroll;
	}
	public Collection<Person> getPayrollPersons() {
		return payrollPersons;
	}
	public double getAuthenticValue() {
		return authenticValue;
	}
	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}
	public void setPayroll(Payroll payroll) {
		this.payroll = payroll;
	}
	public void setPayrollPersons(Collection<Person> payrollPersons) {
		this.payrollPersons = payrollPersons;
	}
	public void setAuthenticValue(double authenticValue) {
		this.authenticValue = authenticValue;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public PayrollService getPayrollService() {
		return payrollService;
	}
	public void setPayrollService(PayrollService payrollService) {
		this.payrollService = payrollService;
	}
	public ChartService getChartService() {
		return chartService;
	}
	public void setChartService(ChartService chartService) {
		this.chartService = chartService;
	}
	public int getGraphicsType() {
		return GraphicsType;
	}
	public void setGraphicsType(int graphicsType) {
		GraphicsType = graphicsType;
	}
}
